package com.harveyguo.nihonngo.service.impl;

import com.harveyguo.nihonngo.model.dto.BoxingGymDto;
import com.harveyguo.nihonngo.model.entity.BoxingGym;
import com.harveyguo.nihonngo.repositories.BoxingGymRepository;
import com.harveyguo.nihonngo.service.BoxingGymService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

/**
 * 拳馆Service实现类
 */
@Service
public class BoxingGymServiceImpl implements BoxingGymService {

    @Autowired
    private BoxingGymRepository boxingGymRepository;

    @Override
    public BoxingGymDto createGym(BoxingGymDto gymDto) {
        // TODO: 实现拳馆创建逻辑
        return gymDto;
    }

    @Override
    public BoxingGymDto findById(String id) {
        Optional<BoxingGym> gym = boxingGymRepository.findById(id);
        if (gym.isPresent()) {
            return convertToDto(gym.get());
        }
        return null;
    }

    @Override
    public List<BoxingGymDto> findAll() {
        return boxingGymRepository.findAll().stream()
                .map(this::convertToDto)
                .toList();
    }

    @Override
    public List<BoxingGymDto> findByVerified(Boolean isVerified) {
        return boxingGymRepository.findByIsVerified(isVerified).stream()
                .map(this::convertToDto)
                .toList();
    }

    @Override
    public List<BoxingGymDto> findByName(String name) {
        return boxingGymRepository.findByNameContaining(name).stream()
                .map(this::convertToDto)
                .toList();
    }

    @Override
    public List<BoxingGymDto> findByAddress(String address) {
        return boxingGymRepository.findByAddressContaining(address).stream()
                .map(this::convertToDto)
                .toList();
    }

    @Override
    public List<BoxingGymDto> findNearbyGyms(Double latitude, Double longitude, Double radius) {
        return boxingGymRepository.findNearbyGyms(latitude, longitude, radius).stream()
                .map(this::convertToDto)
                .toList();
    }

    @Override
    public BoxingGymDto updateGym(String id, BoxingGymDto gymDto) {
        // TODO: 实现拳馆更新逻辑
        return gymDto;
    }

    @Override
    public void deleteGym(String id) {
        boxingGymRepository.deleteById(id);
    }

    @Override
    public BoxingGymDto verifyGym(String id) {
        Optional<BoxingGym> gym = boxingGymRepository.findById(id);
        if (gym.isPresent()) {
            BoxingGym g = gym.get();
            g.setIsVerified(true);
            return convertToDto(boxingGymRepository.save(g));
        }
        return null;
    }

    private BoxingGymDto convertToDto(BoxingGym gym) {
        BoxingGymDto dto = new BoxingGymDto();
        dto.setId(gym.getId());
        dto.setName(gym.getName());
        dto.setAddress(gym.getAddress());
        dto.setPhone(gym.getPhone());
        dto.setDescription(gym.getDescription());
        dto.setBusinessHours(gym.getBusinessHours());
        dto.setFacilities(gym.getFacilities());
        dto.setImages(gym.getImages());
        dto.setLatitude(gym.getLatitude());
        dto.setLongitude(gym.getLongitude());
        dto.setIsVerified(gym.getIsVerified());
        dto.setContactPerson(gym.getContactPerson());
        dto.setEmail(gym.getEmail());
        dto.setWebsite(gym.getWebsite());
        dto.setMaxCapacity(gym.getMaxCapacity());
        dto.setMembershipPlans(gym.getMembershipPlans());
        dto.setCreateTime(gym.getCreateTime());
        dto.setUpdateTime(gym.getUpdateTime());
        return dto;
    }
}